<template>
  <el-dialog v-dialogDrag :visible.sync="dialogVisible" :title="title" width="850px" @close="resetForm('ruleForm')">
    <button-group ref="buttonGroup" @deleteCancel="deleteCancel" @submitForm="submitForm('ruleForm')" @cancel="dialogVisible = false" />
    <div class="formTopFont">维修申请单</div>
    <el-row>
      <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="110px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="申请单号" prop="applyNo">
              <el-input v-model="ruleForm.applyNo" maxlength="20" show-word-limit />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="设备" prop="equipId">
              <el-select v-model="ruleForm.equipId" style="width: 100%">
                <el-option v-for="item in eqAllList" :key="item.id" :label="item.equipName" :value="item.id">
                  <div @click="eqInfo(item)">
                    <span>{{ item.equipName }}</span>
                    <b style="font-weight: normal">{{ item.equipCode ? '【' + item.equipCode + '】' : '' }}</b>
                    <b style="float: right; font-weight: normal">{{ item.type ? '【' + item.type + '】' : '' }}</b>
                  </div>
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="故障分类" prop="faultClassifyId">
              <el-select v-model="ruleForm.faultClassifyId" style="width: 100%">
                <el-option v-for="item in faultClassifyList" :key="item.id" :label="item.classifyName" :value="item.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="故障发生时间" prop="faultTime">
              <el-date-picker v-model="ruleForm.faultTime" :clearable="false" format="yyyy-MM-dd HH:mm:ss" value-format="yyyy-MM-dd HH:mm:ss" type="datetime" placeholder="选择日期" style="width: 100%" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="紧急度" prop="emergency">
              <el-select v-model="ruleForm.emergency" style="width: 100%">
                <el-option label="高" value="高"></el-option>
                <el-option label="中" value="中"></el-option>
                <el-option label="低" value="低"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="申请人" prop="applyBy">
              <el-select v-model="ruleForm.applyBy" style="width: 100%">
                <el-option v-for="item in userAllList" :key="item.id" :label="item.name" :value="item.id" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-form-item label="故障描述" prop="faultRemark">
            <el-input v-model="ruleForm.faultRemark" maxlength="200" type="textarea" rows="3" show-word-limit />
          </el-form-item>
        </el-row>
      </el-form>
    </el-row>
    <bottom-font v-if="title === '编辑'" ref="bottomFont" :obj-name="ruleForm.applyUserName" :obj-date="ruleForm.applyTime" />
  </el-dialog>
</template>
<script>
import bottomFont from '@/components/BottomFont'
export default {
  components: { bottomFont },
  props: {
    faultClassifyList: {
      type: Array,
      default: () => {
        return []
      }
    },
    userAllList: {
      type: Array,
      default: () => {
        return []
      }
    },
    eqAllList: {
      type: Array,
      default: () => {
        return []
      }
    }
  },
  data() {
    const checkApplyNo = async (rule, value, callback) => {
      const res = await this.$axios.get(
        `/equip/repair/order/verifyNoUnique?applyNo=${value}&id=${
          this.ruleForm.id || ''
        }`
      )
      if (res?.data.code === 200) {
        return res.data.data
          ? callback()
          : new Error(callback('申请单号重复！'))
      } else {
        return new Error(callback(res.data.msg))
      }
    }
    return {
      title: '',
      ruleForm: {
        equipId: '',
        applyNo: '',
        emergency: '',
        faultClassifyId: '',
        faultRemark: '',
        faultTime: '',
        applyBy: '',
        id: ''
      },
      rules: {
        equipId: [
          { required: true, message: '设备不能为空！', trigger: 'change' }
        ],
        applyNo: [
          { required: true, message: '申请单号不能为空！', trigger: 'blur' },
          { validator: checkApplyNo, trigger: 'blur' }
        ],
        faultClassifyId: [
          { required: true, message: '故障分类不能为空！', trigger: 'change' }
        ],
        faultTime: [
          { required: true, message: '故障发生时间不能为空！', trigger: 'blur' }
        ],
        applyBy: [
          { required: true, message: '申请人不能为空！', trigger: 'change' }
        ]
      },
      dialogVisible: false,
      obj: {}
    }
  },
  watch: {},
  mounted() {},
  methods: {
    /**
     * @description: 新增编辑
     * @param {*} row
     * @return {*}
     */
    async showDialog(row) {
      this.obj = row
      this.dialogVisible = true
      if (row && row.hasOwnProperty('id')) {
        this.title = '编辑'
        this.ruleForm = Object.assign({}, row)
      } else {
        this.title = '新增'
      }
    },
    /* 设备选择 */
    eqInfo(row) {
      this.ruleForm.equipId = row.id
      this.ruleForm.equipCode = row.equipCode
    },
    /**
     * @description: 表单提交
     * @param {*} formName
     * @return {*}
     */
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          const res = await this.$axios.post(
            '/equip/repair/order/saveOrder',
            this.ruleForm
          )
          if (res?.data.code === 200) {
            this.$notify.success('提交成功！')
            this.dialogVisible = false
            this.$emit('getTableInfo')
          } else {
            this.$notify.error(res.data.msg)
          }
        }
      })
    },
    /* 删除 */
    async deleteCancel() {
      this.$confirm(`确认删除数据？`, '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.$axios
            .delete(`/equip/repair/order/remove/${this.obj.id}`)
            .then((res) => {
              if (res.data.code === 200) {
                this.$notify.success('删除成功！')
                this.dialogVisible = false
                this.$emit('getTableInfo', 1)
              } else {
                this.$notify.error(res.data.msg)
              }
            })
        })
        .catch(() => {})
    },
    /**
     * @description: 重置表单
     * @param {*} formName
     * @return {*}
     */
    resetForm(formName) {
      this.$refs[formName].resetFields()
      this.ruleForm = {
        equipId: '',
        applyNo: '',
        emergency: '',
        faultClassifyId: '',
        faultRemark: '',
        faultTime: '',
        applyBy: '',
        id: ''
      }
    }
  }
}
</script>
<style lang='scss' scoped>
.dialogBtn {
  text-align: right;
}
</style>
